package com.easy.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.*;
import com.easy.common.enumbean.UserStatus;
import com.easy.common.service.IUserService;
import com.easy.user.mapper.RolePermissionMapper;
import com.easy.user.mapper.UserMapper;
import com.easy.user.mapper.UserRoleMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class UserService extends ServiceImpl<UserMapper,SysUser> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public SysUser insert(SysUser sysUser) {
        userMapper.insert(sysUser);
        return userMapper.selectById(sysUser.getId());
    }

    @Override
    public SysUser update(SysUser sysUser) {
        userMapper.updateById(sysUser);
        return userMapper.selectById(sysUser.getId());
    }

    @Override
    public int delete(Integer id) {
        // 先删除用户角色关联记录
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        userRoleMapper.delete(queryWrapper);

        return userMapper.deleteById(id);
    }

    @Override
    public SysUser getById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public IPage<SysUser> getListByPage(IPage page, SysUser sysUser) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>(sysUser);
        return userMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<SysUser> getList(SysUser sysUser) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>(sysUser);
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public int setStatus(Integer id) {
        // 这里假设 SysUser 有一个 setStatus 方法来更新状态
        SysUser user = userMapper.selectById(id);
        UserStatus status = user.getStatus();
        if (status == UserStatus.ENABLED) {
            user.setStatus(UserStatus.DISABLED);
        }else {
            user.setStatus(UserStatus.ENABLED);
        }
        userMapper.updateById(user);
        return 1;
    }

    @Override
    public SysUser assignRole(SysUser sysUser) {
        Integer userId = sysUser.getId();

        // 先删除该用户原有的角色关联
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        userRoleMapper.delete(queryWrapper);

        List<SysRole> roles = sysUser.getRoles();
        for (SysRole role : roles) {
            super.getBaseMapper().insertUserRole(userId, role.getId());
        }
        return super.getById(userId);
    }

    @Override
    public int assignPermission(SysRole sysRole, SysPermission sysPermission) {
        Integer roleId = sysRole.getId();
        Integer permissionId = sysPermission.getId();
        Integer insertCount = 0;

        // 先删除该角色原有的权限关联
        QueryWrapper<SysRolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        rolePermissionMapper.delete(queryWrapper);

        // 再插入新的权限关联
        if (permissionId != null) {
            SysRolePermission rolePermission = new SysRolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            insertCount = rolePermissionMapper.insert(rolePermission);

        }

        return insertCount;
    }

    @Override
    public SysUser getByUsername(String username) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper);
    }



}